<template>
  <div class="schools-container">
    <div class="page-header">
      <h2>学校管理</h2>
      <el-button type="primary" icon="el-icon-plus" @click="$router.push('/school/create')">创建学校</el-button>
    </div>
    
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>学校列表</span>
        <div style="float: right; display: flex;">
          <el-input
            placeholder="搜索学校名称"
            v-model="searchQuery"
            style="width: 200px; margin-right: 10px;"
            prefix-icon="el-icon-search"
            clearable
            @clear="fetchSchools"
            @keyup.enter.native="handleSearch"
          />
          <el-button type="primary" size="small" @click="handleSearch">搜索</el-button>
        </div>
      </div>
      
      <el-table
        :data="schoolList"
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column prop="name" label="学校名称" width="180"></el-table-column>
        <el-table-column prop="code" label="学校代码" width="120"></el-table-column>
        <el-table-column prop="address" label="地址" width="200"></el-table-column>
        <el-table-column prop="contactPerson" label="联系人" width="100"></el-table-column>
        <el-table-column prop="contactPhone" label="联系电话" width="120"></el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
              {{ scope.row.status === 1 ? '已激活' : '未激活' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="$router.push(`/school/edit/${scope.row.id}`)">编辑</el-button>
            <el-button 
              size="mini" 
              :type="scope.row.status === 0 ? 'success' : 'warning'"
              @click="handleStatusChange(scope.row)"
            >
              {{ scope.row.status === 0 ? '激活' : '禁用' }}
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getSchoolList, searchSchools, updateSchoolStatus, deleteSchool } from '@/api/school';

export default {
  name: 'SchoolList',
  data() {
    return {
      schoolList: [],
      loading: false,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      searchQuery: ''
    };
  },
  created() {
    this.fetchSchools();
  },
  methods: {
    fetchSchools() {
      this.loading = true;
      if (this.searchQuery) {
        searchSchools(this.searchQuery, this.currentPage, this.pageSize)
          .then(response => {
            const { data } = response;
            if (data && data.records) {
              this.schoolList = data.records;
              this.total = data.total || 0;
            }
          })
          .finally(() => {
            this.loading = false;
          });
      } else {
        getSchoolList(this.currentPage, this.pageSize)
          .then(response => {
            const { data } = response;
            if (data && data.records) {
              this.schoolList = data.records;
              this.total = data.total || 0;
            }
          })
          .finally(() => {
            this.loading = false;
          });
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchSchools();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchSchools();
    },
    handleSearch() {
      this.currentPage = 1;
      this.fetchSchools();
    },
    handleStatusChange(school) {
      const newStatus = school.status === 0 ? 1 : 0;
      const statusText = newStatus === 1 ? '激活' : '禁用';
      
      this.$confirm(`确认${statusText}该学校吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateSchoolStatus(school.id, newStatus).then(() => {
          this.$message({
            type: 'success',
            message: `${statusText}成功!`
          });
          this.fetchSchools();
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });          
      });
    },
    handleDelete(id) {
      this.$confirm('确认删除该学校吗？此操作不可恢复！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteSchool(id).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.fetchSchools();
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.schools-container {
  .page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    
    h2 {
      margin: 0;
      font-size: 22px;
      font-weight: bold;
    }
  }
  
  .pagination-container {
    margin-top: 20px;
    text-align: right;
  }
}
</style>